package dp_package.all;

import java.util.Scanner;

/**
 * 完全背包
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-08-05 11:00
 */
public class PackageAll {
    /**
     * 二维dp time:n*n space: n*n
     *
     * @param weight        物品重量
     * @param value         物品价值
     * @param packageWeight 背包最大价值
     * @return int
     */
    private static int getMaxValue(int[] weight, int[] value, int packageWeight) {
        int n = weight.length;
        //dp[i]重量i的背包 最大价值
        int[] dp = new int[packageWeight + 1];
        //初始化
//        dp[0]=0;

        //遍历，必须先遍历物品，先遍历背包
        for (int i = 0; i < n; i++) {
            for (int j = weight[i]; j <= packageWeight; j++) {
                dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
            }
//              完全背包需要正序遍历背包
//            正序情况，这次遍历会取得这次遍历的值，可重复放
//            dp[1]=Math.max(dp[1],dp[1-weight[0]] + value[0])
//            dp[2]=Math.max(dp[2],dp[2-weight[0]] + value[0])
//
        }
        return dp[packageWeight];
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//有多少物品
        int packageWeight = scanner.nextInt();//背包最大重量

        int[] weight = new int[n];
        int[] value = new int[n];
        for (int i = 0; i < n; i++) {
            weight[i] = scanner.nextInt();
            value[i] = scanner.nextInt();
        }
//        3 5
//        1 2
//        2 3
//        3 4
//        10
        int maxValue = getMaxValue(weight, value, packageWeight);
        System.out.println(maxValue);
    }
}
